var orderList = {};
var cancelVerification = {};
var OrderList = function () {
    this.dictMap = layuiFunciton.sessionStorageGet("dictMap"); //数据字典
    this.rowData; //选中行数据
    //初始化信息
    this.init = function () {
        //初始化搜索条件 日期范围组件
        layuiFunciton.layTime({
            elem: '#orderDate'
            , range: '~'
            , format: 'yyyy-MM-dd'
            , done: function (value, date) {
                $("#orderDateStart").val(orderList.trim(value.split("~")[0]));
                $("#orderDateEnd").val(orderList.trim(value.split("~")[1]));
            }
        });
        layuiFunciton.layTime({
            elem: '#dateShipment'
            , range: '~'
            , format: 'yyyy-MM-dd'
            , done: function (value, date) {
                $("#dateShipmentStart").val(orderList.trim(value.split("~")[0]));
                $("#dateShipmentEnd").val(orderList.trim(value.split("~")[1]));
            }
        });
        //初始化搜索条件业务员
        layuiFunciton.ajaxRequest({
            url: layuiFunciton.URL + '/sysUser/findUserList',
            data: {page: 1, limit: 99999},
            maskState: false,         //是否开启遮罩
            success: function (data) {
                var optionHtml = '<option value="">请选择</option>';
                data.forEach(function (obj, index) {
                    optionHtml += '<option value="' + obj.name + '">' + obj.name + '</option>';
                });
                $("#salesman").html(optionHtml);
            }
        });
        //初始化数据字典
        //业务类型
        orderList.initSelect("businessTypes", "YWLX_T");
        //品名
        orderList.initSelect("ofTheGoods", "Of_the_goods");
        //箱型
        orderList.initSelect("aBox", "XX");
        //发货单位
        orderList.initSelect("deliveryUnit", "FHDW");
        //发出站
        orderList.initSelect("aStation", "ZD");
        //到站
        orderList.initSelect("arriveStation", "ZD");
        //收货人
        //orderList.initSelect("receivingUnit", "SHDW");
        //装货地点
        orderList.initSelect("standby1", "ZHDD");
        //结算人
        layuiFunciton.ajaxRequest({
            url: layuiFunciton.URL + '/sys/dictVal/getByTypeCode',
            method: 'get',
            maskState: false,
            data: {
                typeCode: "JSR"
            },
            success: function (data) {
                var optionHtml = '<option value="">请选择</option>';
                data.forEach(function (obj, index) {
                    if (obj.valCode == $("#paymentUnit").val()) {
                        optionHtml += '<option value="' + obj.valCode + '" selected>' + obj.valName + '</option>';
                    } else {
                        optionHtml += '<option value="' + obj.valCode + '">' + obj.valName + '</option>';
                    }
                });
                $("#settlementPeople").html(optionHtml);
            }
        });
    };
    this.trim = function (str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    };
    //初始化表格
    this.initTalbe = function () {
        layuiFunciton.tableInit({
            toolbar: '.btn',
            tableId: 'orderListTableId',
            url: layuiFunciton.URL + '/wb/sterilisation/records/findReceivables',
            id: 'orderListId',
            searchFromId: 'searchForm',
            tableFilter: 'orderListFilter',  //表格监听名称,
            cols: [[
                {type: 'checkbox'},
                {field: 'orderCode', width: 120, title: '订单编号', sort: true},
                {field: 'orderDate', title: '下单日期', sort: true}
                , {field: 'dateShipment', title: '发运日期', sort: true}
                , {
                    field: 'businessTypes', title: '业务类型', sort: true,
                    templet: function (row) {
                        if(orderList.dictMap[row.businessTypes]){
                            return orderList.dictMap[row.businessTypes];
                        }else{
                            return "";
                        }
                    }
                }
                , {
                    field: 'ofTheGoods', title: '品名', sort: true,
                    templet: function (row) {
                        if(orderList.dictMap[row.ofTheGoods]){
                            return orderList.dictMap[row.ofTheGoods];
                        }else{
                            return "";
                        }
                    }
                }
                , {
                    field: 'aBox', title: '箱型', sort: true,
                    templet: function (row) {
                        if(orderList.dictMap[row.aBox]){
                            return orderList.dictMap[row.aBox];
                        }else{
                            return "";
                        }
                    }
                }
                , {field: 'cartonNo', title: '箱号', sort: true}
                , {
                    field: 'deliveryUnit', title: '发货单位', sort: true,
                    templet: function (row) {
                        if (orderList.dictMap[row.deliveryUnit]) {
                            return orderList.dictMap[row.deliveryUnit];
                        } else {
                            return "";
                        }
                    }
                }, {
                    field: 'standby1', title: '装货地点', sort: true,
                    templet: function (row) {
                        if (orderList.dictMap[row.standby1]) {
                            return orderList.dictMap[row.standby1];
                        } else {
                            return "";
                        }
                    }
                }
                , {
                    field: 'receivingUnit', title: '收货人', sort: true/*,
                     templet: function (row) {
                     return orderList.dictMap[row.receivingUnit];
                     }*/
                }, {
                    field: 'accountsReceivableAmount', title: '应收金额', sort: true,
                    templet: function (row) {
                        return Number(row.accountsReceivableAmount).toFixed(2);
                    }
                }, {
                    field: 'sterilisationAmount', title: '已收款', sort: true,
                    templet: function (row) {
                        return Number(row.sterilisationAmount).toFixed(2);
                    }
                }, {
                    field: '', title: '欠款', sort: true,
                    templet: function (row) {
                        return (Number(row.accountsReceivableAmount) - Number(row.sterilisationAmount)).toFixed(2);
                    }
                }
                , {
                    field: 'settlementPeople', title: '结算人', sort: true,
                    templet: function (row) {
                        if(orderList.dictMap[row.settlementPeople]){
                            return orderList.dictMap[row.settlementPeople];
                        }else{
                            return "";
                        }
                    }
                }
                , {field: 'goodsWere', title: '货票号', sort: true}
                , {field: 'chargeableWeight', title: '计费重量', sort: true}

            ]],
            tools: {
                confirm_choice: function (row, obj) {
                    var checkData = layuiFunciton.getCheckData('orderListId');
                    if (checkData.length > 0) {
                        cancelVerification.showData(checkData);
                        layuiFunciton.closeOpen(cancelVerification.opneLayer);
                    } else {
                        layuiFunciton.warn('至少选择一行');
                    }
                }
                //点击一行
                /*, orderListFilter: function (row, obj) {
                 var checkbox = obj.tr.find('input[type="checkbox"]');
                 if (checkbox.attr("checked")) {
                 checkbox.prop("checked", false);
                 } else {
                 checkbox.prop("checked", true);
                 }
                 form.render('checkbox');
                 }*/
            }
        });
    };
    //获取当月第一天
    this.getCurrentMonthFirst = function () {
        var date = new Date();
        date.setDate(1);
        return orderList.dateFtt("yyyy-MM-dd", date);
    };
    //获取当月最后一天
    this.getCurrentMonthLast = function () {
        var date = new Date();
        var currentMonth = date.getMonth();
        var nextMonth = ++currentMonth;
        var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
        var oneDay = 1000 * 60 * 60 * 24;
        return orderList.dateFtt("yyyy-MM-dd", new Date(nextMonthFirstDay - oneDay));
    };
    //时间格式化
    this.dateFtt = function (fmt, date) {
        var o = {
            "M+": date.getMonth() + 1,     //月份
            "d+": date.getDate(),     //日
            "h+": date.getHours(),     //小时
            "m+": date.getMinutes(),     //分
            "s+": date.getSeconds(),     //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds()    //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    };
    this.event = function () {
        // 控制更多查询条件的显示和隐藏
        $(".layui-searchM").hide();
        $(".layui-search-more").click(function (event) {
            if ($(".layui-searchM").css('display') == "none") {
                $(".layui-search-more .layui-icon").html("&#xe619;");
            } else {
                $(".layui-search-more .layui-icon").html("&#xe61a;");
            }
            $(".layui-searchM").slideToggle();
        });
        $("#search").click(function () {
            layuiFunciton.refresh('searchForm', 'orderListId');
        });
        $("#reload").click(function () {
            $("#orderDateStart").val("");
            $("#orderDateEnd").val("");
            $("#dateShipmentStart").val("");
            $("#dateShipmentEnd").val("");
        });
    };
    //初始化input 搜索
    this.initSelect = function (item,typeCode) {
        $("#"+item).attr("readonly","");
        //引入自定义插件
        layui.config({
            base: '/public/plugin/tableSelect/' //设置自定义插件路径:根据实际路径进行设置
        }).use(['tableSelect'], function() {
            var tableSelect = layui.tableSelect;
            tableSelect.render({
                elem: '#'+item,	//定义输入框input对象
                checkedKey: 'valCode', //表格的唯一建值，非常重要，影响到选中状态 必填
                searchKey: 'valName',	//搜索输入框的name值 默认keyword
                searchPlaceholder: '关键词搜索',	//搜索输入框的提示文字 默认关键词搜索
                table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                    url:layuiFunciton.URL + '/sys/dictVal/getByTypeCode?typeCode='+typeCode,
                    page:false,
                    cols: [[
                        {field: 'valName', title: '请选择（双击）',align: "center"}
                    ]]
                },
                done: function (elem, data) {
                    $('#'+item).val(data.data[0].valName);
                    $('#'+item).siblings("input[type='hidden']").val(data.data[0].valCode);
                }
            });
            var dictMap = layuiFunciton.sessionStorageGet("dictMap"); //数据字典
            if($('#'+item).val()){
                $("#" + item).after('<input type="hidden" name="'+item+'" value="'+$('#'+item).val()+'" >');
                $('#'+item).val(dictMap[$('#'+item).val()]);
            }else{
                $("#" + item).after('<input type="hidden" name="'+item+'" value="" >');
            }
        });
    }
};
function pageReady(obj) {
    cancelVerification = obj;
    orderList = new OrderList();
    orderList.init();
    orderList.initTalbe();
    orderList.event();
}
